home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / sun / awt / AWTAutoShutdown.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  3.0 KB  |  169 lines

  1. package sun.awt;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.util.HashSet;
  5. import java.util.Hashtable;
  6.  
  7. public final class AWTAutoShutdown implements Runnable {
  8.    private static final AWTAutoShutdown theInstance = new AWTAutoShutdown();
  9.    private final Object mainLock = new Object();
  10.    private final Object activationLock = new Object();
  11.    private final HashSet busyThreadSet = new HashSet(7);
  12.    private boolean toolkitThreadBusy = false;
  13.    private final Hashtable peerMap = new PeerMap();
  14.    private Thread blockerThread = null;
  15.    private boolean timeoutPassed = false;
  16.    private static final int SAFETY_TIMEOUT = 1000;
  17.  
  18.    private AWTAutoShutdown() {
  19.    }
  20.  
  21.    public static AWTAutoShutdown getInstance() {
  22.       return theInstance;
  23.    }
  24.  
  25.    public static void notifyToolkitThreadBusy() {
  26.       getInstance().setToolkitBusy(true);
  27.    }
  28.  
  29.    public static void notifyToolkitThreadFree() {
  30.       getInstance().setToolkitBusy(false);
  31.    }
  32.  
  33.    public void notifyThreadBusy(Thread var1) {
  34.       synchronized(this.activationLock) {
  35.          synchronized(this.mainLock) {
  36.             if (this.blockerThread == null) {
  37.                this.activateBlockerThread();
  38.             } else if (this.isReadyToShutdown()) {
  39.                this.mainLock.notifyAll();
  40.                this.timeoutPassed = false;
  41.             }
  42.  
  43.             this.busyThreadSet.add(var1);
  44.          }
  45.  
  46.       }
  47.    }
  48.  
  49.    public void notifyThreadFree(Thread var1) {
  50.       synchronized(this.activationLock) {
  51.          synchronized(this.mainLock) {
  52.             this.busyThreadSet.remove(var1);
  53.             if (this.isReadyToShutdown()) {
  54.                this.mainLock.notifyAll();
  55.                this.timeoutPassed = false;
  56.             }
  57.          }
  58.  
  59.       }
  60.    }
  61.  
  62.    void notifyPeerMapUpdated() {
  63.       synchronized(this.activationLock) {
  64.          synchronized(this.mainLock) {
  65.             if (!this.isReadyToShutdown() && this.blockerThread == null) {
  66.                this.activateBlockerThread();
  67.             } else {
  68.                this.mainLock.notifyAll();
  69.                this.timeoutPassed = false;
  70.             }
  71.          }
  72.  
  73.       }
  74.    }
  75.  
  76.    private boolean isReadyToShutdown() {
  77.       return !this.toolkitThreadBusy && this.peerMap.isEmpty() && this.busyThreadSet.isEmpty();
  78.    }
  79.  
  80.    private void setToolkitBusy(boolean var1) {
  81.       if (var1 != this.toolkitThreadBusy) {
  82.          synchronized(this.activationLock) {
  83.             synchronized(this.mainLock) {
  84.                if (var1 != this.toolkitThreadBusy) {
  85.                   if (var1) {
  86.                      if (this.blockerThread == null) {
  87.                         this.activateBlockerThread();
  88.                      } else if (this.isReadyToShutdown()) {
  89.                         this.mainLock.notifyAll();
  90.                         this.timeoutPassed = false;
  91.                      }
  92.  
  93.                      this.toolkitThreadBusy = var1;
  94.                   } else {
  95.                      this.toolkitThreadBusy = var1;
  96.                      if (this.isReadyToShutdown()) {
  97.                         this.mainLock.notifyAll();
  98.                         this.timeoutPassed = false;
  99.                      }
  100.                   }
  101.                }
  102.             }
  103.          }
  104.       }
  105.  
  106.    }
  107.  
  108.    public void run() {
  109.       Thread var1 = Thread.currentThread();
  110.       boolean var2 = false;
  111.       synchronized(this.mainLock) {
  112.          try {
  113.             this.mainLock.notifyAll();
  114.  
  115.             while(this.blockerThread == var1) {
  116.                this.mainLock.wait();
  117.                this.timeoutPassed = false;
  118.  
  119.                while(this.isReadyToShutdown()) {
  120.                   if (this.timeoutPassed) {
  121.                      this.timeoutPassed = false;
  122.                      this.blockerThread = null;
  123.                      break;
  124.                   }
  125.  
  126.                   this.timeoutPassed = true;
  127.                   this.mainLock.wait(1000L);
  128.                }
  129.             }
  130.          } catch (InterruptedException var10) {
  131.             var2 = true;
  132.          } finally {
  133.             if (this.blockerThread == var1) {
  134.                this.blockerThread = null;
  135.             }
  136.  
  137.          }
  138.       }
  139.  
  140.       if (!var2) {
  141.          AppContext.stopEventDispatchThreads();
  142.       }
  143.  
  144.    }
  145.  
  146.    static AWTEvent getShutdownEvent() {
  147.       return new 1(getInstance(), 0);
  148.    }
  149.  
  150.    private void activateBlockerThread() {
  151.       Thread var1 = new Thread(this, "AWT-Shutdown");
  152.       var1.setDaemon(false);
  153.       this.blockerThread = var1;
  154.       var1.start();
  155.  
  156.       try {
  157.          this.mainLock.wait();
  158.       } catch (InterruptedException var3) {
  159.          System.err.println("AWT blocker activation interrupted:");
  160.          var3.printStackTrace();
  161.       }
  162.  
  163.    }
  164.  
  165.    public Hashtable getPeerMap() {
  166.       return this.peerMap;
  167.    }
  168. }
  169.